package com.fenghuajueli.libbasecoreui.utils;

import android.app.Activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ActivityUtils {

    private static List<Activity> activityList = new ArrayList<>();

    /**
     * 添加一个activity到列表里
     *
     * @param activity
     */
    public static void addToStack(Activity activity) {
        activityList.add(activity);
    }


    /**
     * 关闭一个activity
     *
     * @param classz
     */
    public static void finishActivity(Class classz) {
        if (activityList != null) {
            Iterator<Activity> iterator = activityList.iterator();
            while (iterator.hasNext()) {
                Activity target = iterator.next();
                if (target.getClass().equals(classz)) {
                    iterator.remove();
                    target.finish();
                }
            }
        }
    }


    /**
     * 关闭所有的activty,但除去某一个
     * @param classez
     */
    public static void finishAllActivityExceptClass(Class classez){
        if (activityList != null) {
            Iterator<Activity> iterator = activityList.iterator();
            while (iterator.hasNext()) {
                Activity target = iterator.next();
                if (!target.getClass().equals(classez)) {
                    iterator.remove();
                    target.finish();
                }
            }
        }
    }

}
